<?php
/**
 * Description of PessoaDAO
 *
 * @author Magno
 */
class BonusDAO {

    private $conexao;
    public function __construct() {
        $this->conexao = Conexao::getConexao();
    }

    public function salvar($bonus) {

        try {

            if($bonus->getBonusID() != null && $bonus->getBonusID() > 0) {
                $statment = $this->conexao->prepare("UPDATE bonus SET
                        descricao = :descricao,                        
                        valor = :valor
                        WHERE bonusID = :bonusID");

            }else {
                $statment = $this->conexao->prepare("INSERT INTO bonus(bonusID,descricao,valor) VALUES (:bonusID,:descricao,:valor);");
            }

            if($statment->execute($bonus->toBD())) {

                if($bonus->getBonusID() == null || $bonus->getBonusID() <= 0)
                    $bonus->setBonusID($this->conexao->lastInsertId());

                return $bonus;
            }else {
                $erros = $statment->errorInfo();
                throw new Exception("Erro BonusDAO: ".$erros[2]);
            }

        }catch(PDOException $err) {
            throw new Exception("Erro BonusDAO:".$err->getMessage());
        }

    }

    public function excluir($bonusID) {

        try {

            $statment = $this->conexao->prepare("DELETE FROM bonus WHERE bonusID = :bonusID");
            $statment->bindParam(':bonusID',$bonusID);

            if(!$statment->execute()) {
                $erros = $statment->errorInfo();
                throw new Exception("Erro BonusDAO: ".$erros[2]);
            }

        }catch(PDOException $err) {
            throw new Exception("Erro BonusDAO:".$err->getMessage());
        }

    }

    public function listar($pagina = 0,$descricao = "") {
        try {

            $limite = Util::getPaginacao($pagina);
            $where = $this->getWhere($descricao);
            $bonuss = array();
            $statment = $this->conexao->query( "SELECT * FROM bonus e $where $limite" );

            foreach ( $statment->fetchAll(PDO::FETCH_SERIALIZE) as $row )
                $bonuss[] = new BonusBean($row['bonusID'], $row['descricao'], $row['valor']);

            return $bonuss;

        }catch(PDOException $err) {
            throw new Exception("Erro BonusDAO:".$err->getMessage());
        }
    }

    public function listarPorOrdem($pagina = 0, $tamanho = 0, $ordem = "bonusID", $direcao = "DESC") {
        try {

            $limite = Util::getPaginacao($pagina,$tamanho);
            $bonuss = array();
            $statment = $this->conexao->query("SELECT * FROM bonus e ORDER BY u.$ordem $direcao $limite");

            foreach ($statment->fetchAll(PDO::FETCH_SERIALIZE) as $row){
                $bonuss[] = new BonusBean($row['bonusID'], $row['descricao'], $row['valor']);
            }

            return $bonuss;
        } catch (PDOException $err) {
            throw new Exception("Erro EnqueteDAO:" . $err->getMessage());
        }
    }

    public function buscarPorID($bonusID) {
        try {

            $row = $this->conexao->query( "SELECT * FROM bonus WHERE bonusID = $bonusID " )->fetch(PDO::FETCH_SERIALIZE);

            if(isset($row['bonusID']) && $row['bonusID'] > 0) {
                $bonus = new BonusBean($row['bonusID'], $row['descricao'], $row['valor']);
                return $bonus;
            }else {
                return null;
            }

        }catch(PDOException $err) {
            throw new Exception("Erro BonusDAO:".$err->getMessage());
        }
    }

    private function getWhere($descricao = "") {
        $where = " WHERE 1 = 1 ";

        if (isset ($descricao) && strlen($descricao) > 0) {
            $where = $where." AND e.descricao = '$descricao' ";
        }

        return $where;
    }

    public function total($descricao = "") {
        try {

            $where = $this->getWhere($descricao);
            $total = $this->conexao->query( "SELECT COUNT(e.bonusID) total FROM bonus e $where" )->fetch(PDO::FETCH_COLUMN);
            return $total;

        }catch(PDOException $err) {
            throw new Exception("Erro BonusDAO:".$err->getMessage());
        }
    }

}
?>